class EventsController < ApplicationController

	def test
		@events = Event.order("events.start_date DESC")
		@event = Event.find(params[:id])
	end

	def show
		@events = Event.order("events.start_date DESC")
		@event_show = Event.find(params[:id])
		@event = Event.new
		$errors = false
	end

	def new
		@events = Event.order("events.start_date DESC")
		@event = Event.new
	end
	
	def index
		@events = Event.order("events.start_date DESC")
		@event_show = @events.first
		@event = Event.new
		$errors = false
	end
	
	def create
		@event = Event.create(params[:event])
		@event.start_date = params[:event][:start_date].to_date
		@event.end_date = params[:event][:end_date].to_date
		if @event.save
			$errors = false
			redirect_to(:action => "show", :id => @event.id)
		else
			$errors = true
			@events = Event.order("events.start_date DESC")
			@event_show = @events.first
			render "index"
		end
	end
	
	def edit
		@event = Event.find(params[:id])
	end

	def update
		@event = Event.find(params[:id])
		if @event.update_attributes(params[:event])
			redirect_to :action => "show", :id => params[:id]
		else
			render "edit"
		end
	end

	def destroy
		@event = Event.find(params[:id])

	if @event.destroy
			flash[:notice] = "Event Deleted $blue"
			redirect_to(:action =>  "index")
	else
		flash[:notice] = "Couldn't delete Event $red"
	end
	end

end
